package com.zc.iotDevice.service.impl;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.StringUtils;
import com.zc.iotDevice.domain.ChargeStationPile;
import com.zc.iotDevice.domain.ChargeStationPort;
import com.zc.iotDevice.mapper.ChargeStationPileMapper;
import com.zc.iotDevice.mapper.ChargeStationPortMapper;
import com.zc.iotDevice.service.ChargeStationPortService;
import com.zc.manager.device.entity.DeviceProperty;
import com.zc.remoteControl.service.RemoteInstructSendService;
import com.zc.utils.ImageConvertBase64;
import com.zc.utils.QRCodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChargeStationPortServiceImpl implements ChargeStationPortService {

    @Resource
    private ChargeStationPortMapper chargeStationPortMapper;
    @Resource
    private ChargeStationPileMapper pileMapper;
    @Resource
    private RemoteInstructSendService remoteInstructSendService;

    @Override
    public int deleteByPrimaryKey(String id) {
        return chargeStationPortMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ChargeStationPort record) {
        ChargeStationPile pile = pileMapper.selectByPrimaryKey(record.getDeviceId());
        if (pile == null) {
            throw new RuntimeException("未知充电桩");
        }
        ChargeStationPort port = new ChargeStationPort();
        port.setDeviceId(record.getDeviceId());
        List<ChargeStationPort> list = chargeStationPortMapper.getList(port);
        if (list.size() >= pile.getPortNum()) {
            throw new RuntimeException("此充电桩已绑最大数量");
        }
        record.setStationId(pile.getStationId());
        record.setStationCode(pile.getStationCode());
        //枪二维码下发
        distributeQRCode(record);
        return chargeStationPortMapper.insert(record);
    }

    @Override
    public int insertSelective(ChargeStationPort record) {
        return chargeStationPortMapper.insertSelective(record);
    }

    @Override
    public ChargeStationPort selectByPrimaryKey(String id) {
        return chargeStationPortMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ChargeStationPort record) {
        //枪二维码下发
        distributeQRCode(record);
        return chargeStationPortMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ChargeStationPort record) {
        return chargeStationPortMapper.updateByPrimaryKey(record);
    }

    //枪二维码下发
    private void distributeQRCode(ChargeStationPort record) {
        //查询枪口
        ChargeStationPort port = new ChargeStationPort();
        port.setDeviceId(record.getDeviceId());
        //生成二维码
        String gunPos = record.getId();
        String content = "https://www.sdzctech.cn:4433/appQrCode?id=" + record.getStationId() + "&code=" + record.getStationCode() +
                "&deviceId=" + record.getDeviceId() + "&gunPos=" + gunPos;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QRCodeUtil.createCodeToOutputStream(content, outputStream);
        String QrCode = ("data:image/png;base64," + ImageConvertBase64.bytesEncode2Base64(outputStream.toByteArray()));
        Map<String, Object> stringObjectMap =
                remoteInstructSendService.distributeQRCode(record.getDeviceId(), gunPos.substring(gunPos.length() - 2), QrCode, "YTSN-CDZ-N01");

    }

    @Override
    public List<ChargeStationPort> getList(ChargeStationPort port) {
        List<ChargeStationPort> list = chargeStationPortMapper.getList(port);
        /*List<ChargeStationPort> data =new ArrayList<>();
        list.forEach(p->{
            String status="0";
            if(!"offline".equals(p.getDeviceState())){
                Map<String, DeviceProperty> realData = remoteInstructSendService.getRealData(p.getId());
                if(realData.containsKey("runningState")){
                    String AStatic = String.valueOf(realData.get("runningState").getValue());
                    if("1".equals(AStatic)){
                        status="3";
                    }else if("2".equals(AStatic)){
                        status="1";
                    }else if("3".equals(AStatic)){
                        status="2";
                    }
                }
            }
            p.setServiceStatus(status);
            if(StringUtils.isNotEmpty(port.getServiceStatus())&&p.getServiceStatus().equals(port.getServiceStatus())){
                data.add(p);
            }
        });
        System.out.println(new PageInfo(list).getTotal());
        System.out.println(new PageInfo(data).getTotal());
        if(StringUtils.isNotEmpty(port.getServiceStatus())){
            return data;
        }else{
            return list;
        }*/
        return list;
    }

    @Override
    public Map<String, Integer> getStatus(BaseEntity entity) {
        List<ChargeStationPort> lists = getList(new ChargeStationPort());
        int total = lists.size();
        int freeNum = 0;
        int offLineNum = 0;
        int useNum = 0;
        for (int i = 0; i < lists.size(); i++) {
            if ("1".equals(lists.get(i).getServiceStatus())) {
                freeNum = freeNum + 1;
            } else if ("2".equals(lists.get(i).getServiceStatus())) {
                useNum = useNum + 1;
            } else {
                offLineNum = offLineNum + 1;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("total", total);
        map.put("freeNum", freeNum);
        map.put("offLineNum", offLineNum);
        map.put("useNum", useNum);
        return map;

        //return chargeStationPortMapper.getStatus(entity);
    }

    @Override
    public int updateDelState(String id) {
        return chargeStationPortMapper.updateDelState(id);
    }
}
